package com.lin.manager.login.service.impl;

import com.lin.manager.login.service.SysCodeService;
import com.lin.manager.redis.constants.RedisConstants;
import com.lin.manager.redis.service.RedisService;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.concurrent.TimeUnit;

/**
 * @author linjiang
 * @date 2024/7/26
 * @fn
 */
@Slf4j
@Service("codeService")
public class SysCodeServiceImpl implements SysCodeService {
    @Resource
    RedisService redisService;

    @Override
    public Boolean verifyCode(String code) {
        String cacheCode = redisService.getStr(RedisConstants.SYS_LOGIN_CAPTCHA.concat(code));
        return code.equalsIgnoreCase(cacheCode);
    }

    @Override
    public void removeCode(String code) {
        redisService.delete(RedisConstants.SYS_LOGIN_CAPTCHA.concat(code));
    }

    @Override
    public String getVerfyCode() {
        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 38, 4);
        // 设置字体 有默认字体，可以不用设置
        specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));
        // 设置类型，纯数字、纯字母、字母数字混合
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        String code = specCaptcha.toBase64();
        String text = specCaptcha.text();
        log.info("验证码文本，{},图片验证码：{}", text, code);
        redisService.setExpStr(RedisConstants.SYS_LOGIN_CAPTCHA.concat(text), text, 1L, TimeUnit.MINUTES);

        return code;
    }
}
